import { Engine } from "php-parser";
import Fs from "fs/promises";
import Comments from "./comments";
import { type varType } from "./base";

const engine: Engine = new Engine({
    parser: {
        extractDoc: true,
        php8: true,
    },
    ast: {
        withPositions: true,
    }
})

/**
 * 解析PHP文件
 * @param file 目标文件
 * @param out_file 导出文件
 * @returns void
 */
export const Parser = async (file: string, out_file: string) => {
    const readFile = await Fs.readFile(file)
    const ast = engine.parseCode(readFile.toString(), file)

    const json_file = out_file.replace(".cpp", ".json")
    Bun.write(json_file, JSON.stringify(ast))

    const cppCode = new Program(ast.children).getCode()
    Bun.write(out_file, cppCode)

}

class Program {

    // 变量列表
    vars: varType[] = [
        { "type": "int", "var": "a" },
    ]

    // cpp代码
    code: string = ""

    constructor(node: Array<any>) {
        for (const item of node) {
            if (item.kind == "expressionstatement") {
                if (typeof item.leadingComments != "undefined") {
                    const comments: varType[] = new Comments(item.leadingComments, this.vars).value()

                    
                }
            }
        }
    }



    /**
     * 获取代码
     * @returns string
     */
    public getCode() {
        return this.code
    }

}